package com.example.zuo.mysuperwidgetdemo.adapter.superwrapper;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zuo.mysuperwidgetdemo.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;
import com.zhy.adapter.recyclerview.wrapper.LoadMoreWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ActionCenter.CONVERT_COMMAND;
import static com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ActionCenter.FETCH_COMMAND;
import static com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ActionCenter.FETCH_COMMAND_FOR_SLAYOUT;


/**
 * Created by a1800 on 4/1/2017.
 */
public class SuperAdapterWrapper<T extends Object> {

    RecyclerView rView;
    SwipeRefreshLayout sLayout;
    RecyclerView.ItemDecoration item;


    public static final int DEFALUT_MODE = 1;
    public static final int CUSTOM_MODE = 2;


    FetchAction fetchAction;

    FetchAction sLayoutFetchAction;

    ConvertAction convertAction;

    int adapterType = 1;

    int itemLayoutRes;

    int PAGESIZE = 10;

    RecyclerView.LayoutManager manager;

    List<T> datas;

    HashMap<String, BaseAction> actionMap;

    RecyclerView.Adapter adapter;

    Context context;

    boolean needLoadMore;

    LinearLayout loadView;

    LoadMoreWrapper mLoadWrapper;

    void init() {
        rView.setLayoutManager(manager);
        if (item != null)
            rView.addItemDecoration(item);

        datas = new ArrayList<>();
        fetchAction = (FetchAction) actionMap.get(FETCH_COMMAND);
        sLayoutFetchAction = (FetchAction) actionMap.get(FETCH_COMMAND_FOR_SLAYOUT);
        convertAction = (ConvertAction) actionMap.get(CONVERT_COMMAND);
        if (sLayout != null) {
            sLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    if (fetchAction != null)
                        fetchAction.fetch(true);
                    else
                        sLayoutFetchAction.fetch(true);
                }
            });
        }
        datas = new ArrayList<>();
        if (adapterType == DEFALUT_MODE) {
            adapter = new CommonAdapter<T>(context, itemLayoutRes, datas) {
                @Override
                protected void convert(ViewHolder holder, T t, int position) {
                    convertAction.convert(holder, t, position);
                }
            };
        }

        loadView = (LinearLayout) View.inflate(context, R.layout.item_test, null);
        ((TextView) loadView.findViewById(R.id.text)).setText("正在加载");

    }

    public void wrap(RecyclerView.Adapter adapter) {
        this.adapter = adapter;

    }

    public List<T> getDatas() {
        return datas;
    }

    private void toggleRefresh(boolean refresh) {
        if (sLayout == null)
            return;
        if (sLayout.isRefreshing()) {
            sLayout.setRefreshing(refresh);
        }

    }

    public void dataChange() {
        rView.getAdapter().notifyDataSetChanged();
    }

    EmptyWrapper emptyWrapper;

    public void handleData(List<T> datas, boolean isRefresh) {
        toggleRefresh(false);


        if (isRefresh)
            this.datas.clear();
        if (datas != null)
            this.datas.addAll(datas);


        if (emptyWrapper == null) {
            emptyWrapper = new EmptyWrapper(adapter);
            emptyWrapper.setEmptyView(R.layout.item_empty);
            rView.setAdapter(emptyWrapper);
        }

        if (datas == null || datas.size() < 10) {
            emptyCheck();

        } else if (datas.size() >= 10) {
            if (fetchAction != null) {

                if (mLoadWrapper == null) {
                    mLoadWrapper = new LoadMoreWrapper(emptyWrapper);
                    mLoadWrapper.setLoadMoreView(loadView);
                    mLoadWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
                        @Override
                        public void onLoadMoreRequested() {
                            fetchAction.fetch(false);
                        }
                    });
                    rView.setAdapter(mLoadWrapper);

                }
            }


        }

        rView.getAdapter().notifyDataSetChanged();

    }


    void emptyCheck() {

        if (mLoadWrapper != null) {
            rView.setAdapter(emptyWrapper);
            mLoadWrapper = null;
        }


    }
}
